package softwaredepartment.zxit.com.pt_3280cinandroid.Service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;

import com.socks.library.KLog;

import java.io.IOException;
import java.util.ArrayList;

import softwaredepartment.zxit.com.pt_3280cinandroid.Entity.MusicInfo;

public class MusicService extends Service {
    public final IBinder binder = new MyBinder();

    public ArrayList<MusicInfo> getMusicDir() {
        return musicDir;
    }

    public void setMusicDir(ArrayList<MusicInfo> musicDir) {
        this.musicDir = musicDir;
    }

    private ArrayList<MusicInfo> musicDir;
    private int musicIndex = 0;
    public static MediaPlayer mp = new MediaPlayer();
    public static Boolean I$Initial = false;

    public MusicService() {
        musicDir = new ArrayList<>();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        //throw new UnsupportedOperationException("Not yet implemented");
        return binder;
    }


    public void addMusic(MusicInfo MusicPath) {
        musicDir.add(MusicPath);
        try {
            mp.reset();
            mp.setDataSource(musicDir.get(musicDir.size()-1).getMusicPath());
            mp.prepare();
            I$Initial = true;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void playOrPause() {
        if (mp.isPlaying()) {
            mp.pause();
        } else {
            mp.start();
        }
    }

    public void stop() {
        if (mp != null) {
            mp.stop();
            try {
                mp.prepare();
                mp.seekTo(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void nextMusic() {
        if (mp != null && musicIndex < 3) {
            mp.stop();
            try {
                mp.reset();
                mp.setDataSource(musicDir.get(musicIndex + 1).getMusicPath());
                musicIndex++;
                mp.prepare();
                mp.seekTo(0);
                mp.start();
            } catch (Exception e) {
                KLog.d("hint", "can't jump next music");
                e.printStackTrace();
            }
        }
    }

    public void preMusic() {
        if (mp != null && musicIndex > 0) {
            mp.stop();
            try {
                mp.reset();
                mp.setDataSource(musicDir.get(musicIndex - 1).getMusicPath());
                musicIndex--;
                mp.prepare();
                mp.seekTo(0);
                mp.start();
            } catch (Exception e) {
                KLog.d("hint", "can't jump pre music");
                e.printStackTrace();
            }
        }
    }

    public class MyBinder extends Binder {
        public MusicService getService() {
            return MusicService.this;
        }

    }
}
